package simulateOS.UI;

import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource;
import simulateOS.MainApplication;

import java.io.IOException;

/**
 * Description：进程、内存等界面
 * <p>Date: 2022/10/31
 * <p>Time: 8:37
 *
 * @Author SillyBaka
 **/
@Slf4j
public class ProcessAndMemoryUI {

    private static volatile Stage processStage;

    /**
     * 获取进程界面的主舞台
     */
    public static Stage getProcessStage(){
        if(processStage == null){
            synchronized (ProcessAndMemoryUI.class){
                if(processStage == null){
                    initProcessStage();
                }
            }
        }
        return processStage;
    }

    /**
     * 初始化进程界面舞台
     */
    private static void initProcessStage(){

        try {
            processStage = new Stage();
            ConfigurableApplicationContext applicationContext = MainApplication.applicationContext;

            Resource processResource = applicationContext.getResource("processPage.fxml");
            FXMLLoader fxmlLoader = new FXMLLoader(processResource.getURL());
            fxmlLoader.setControllerFactory((applicationContext::getBean));

            Resource deviceResource = applicationContext.getResource("DeviceApp.fxml");
            FXMLLoader deviceLoader = new FXMLLoader(deviceResource.getURL());
            deviceLoader.setControllerFactory((applicationContext::getBean));

            AnchorPane root = fxmlLoader.load();
            Scene scene = new Scene(root);

            AnchorPane devicePane = deviceLoader.load();
            root.getChildren().add(devicePane);
            AnchorPane.setLeftAnchor(devicePane,50D);
            AnchorPane.setTopAnchor(devicePane,180D);

            processStage.setScene(scene);


        } catch (IOException e) {
            log.error("加载进程界面的舞台失败",e);
        }
    }
}
